package modelLayer;

import java.io.IOException;
import java.util.ArrayList;

import uiLayer.SaveToFile;

public class LeaseProductsRecords {
	private ArrayList<ProductForLease> itemsForLoan;
	private static LeaseProductsRecords instance = null; 
	private int id ;

	public LeaseProductsRecords(){
	itemsForLoan = new ArrayList<ProductForLease>();
	id = 0;
	}
public static LeaseProductsRecords getInstance(){
	if(instance == null){
		instance = new LeaseProductsRecords();
	}
	return instance;
}
public void addLItem(ProductForLease product){
	itemsForLoan.add(product);
}

public int createLItem(String name, String description, double price, double quantity, String category, String state){
	id++;
	String location = "DIY Centre";
	ProductForLease product = new ProductForLease(id,name,description,price,quantity,category,location,state);
	addLItem(product);	
	return id;
}

public ProductForLease getProduct(int id){
	for(ProductForLease p1:itemsForLoan){
		if(p1.getId()==id){
			return p1;
		}
	}
	return null;
}

public String findLProduct(int id){
	return getProduct(id).toString();
}

public void updateLProduct(int id, double quantity, double price){
	ProductForLease p1 = getProduct(id);
	if(quantity!=0)p1.setQuantity(quantity);
	if(price!=0)p1.setPrice(price);
}

public void deleteProduct(int id){
	itemsForLoan.remove(getProduct(id));
}

public ArrayList<ProductForLease> getItemsForLoan() {
	return itemsForLoan;
}

public void save() throws IOException{
	for(ProductForLease pfl: itemsForLoan){
		SaveToFile.getInstance().save(pfl.save());
	}
}
}
